package com.example.design;

/**
 * 代理模式，代码演示
 * @author xiaohf
 * @createTime 2021/9/27 21:09
 **/
public class Proxy {
    public static void main(String[] args) {
        //基于接口实现代理
        Subject subject = new RealSubjectProxy();
        String request = subject.request();
        System.out.println("接口实现代理" + request);

        //基于继承实现代理
        RealSubject realSubject = new RealSubjectExtendsProxy();
        System.out.println("继承实现代理" + realSubject.request());
    }
}

interface Subject{
    public String request();
}

class RealSubject implements Subject{

    @Override
    public String request() {
        return "处理请求";
    }
}

class RealSubjectExtendsProxy extends RealSubject {
    @Override
    public String request() {
        System.out.println("代理对象的处理逻辑");
        return super.request();
    }
}



class RealSubjectProxy implements Subject{
    RealSubject realSubject = new RealSubject();
    @Override
    public String request() {
        System.out.println("代理对象的处理逻辑");
        String request = realSubject.request();
        System.out.println("代理对象的处理逻辑-后");
        return request;
    }
}
